/**
 * Copyright &copy; 2012-2014 hongsuan.pplive All rights reserved.
 */
package com.hongsuan.pplive.modules.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.Compass;
import org.compass.core.CompassException;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchHelper;
import org.compass.core.support.search.CompassSearchResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.hongsuan.pplive.modules.cms.dao.ArticleDao;
import com.hongsuan.pplive.modules.cms.entity.Article;

/**
 * 文章搜索Service
 * @author Susan
 * @version 2015-11-09
 */
@Service
@Transactional(readOnly = true)
public class ArticleSearchService {
    @Autowired
    private CompassTemplate compassTemplate;
    @Autowired
    private ArticleDao dao;

    /**
     * 全量建索引
     */
    public void createIndex(List<Article> allList){
        Compass compass = compassTemplate.getCompass();
        CompassSession session = compass.openSession();

        for(Article articleToSave : allList){
            session.save(articleToSave);
        }
        session.commit();
    }

    /**
     * 删除索引
     */
    public void deleteIndex(Article article) {
        CompassSession session = null;
        try {
            session = compassTemplate.getCompass().openSession();
            session.delete(article);
            session.commit();
        } catch (CompassException e) {
            e.printStackTrace();

            if(session!=null && !session.isClosed()) session.rollback();
        }finally{
            if(session!=null && !session.isClosed()) session.close();
        }
    }

    /**
     * 更新单个索引
     */
    public void updateIndex(Article article) {
        CompassSession session = null;
        try {
            session = compassTemplate.getCompass().openSession();
            session.delete(article);
            session.save(article);
            session.commit();
        } catch (CompassException e) {
            e.printStackTrace();

            if(session!=null && !session.isClosed()) session.rollback();
        }finally{
            if(session!=null && !session.isClosed()) session.close();
        }
    }

	/**
	 * 全文检索
	 */
	public Page<Article> search(Page<Article> page, String q, String categoryId, String beginDate, String endDate){

        Compass compass = compassTemplate.getCompass();
        CompassSession session = compass.openSession();
        ArrayList<Article> list = new ArrayList<Article>();

        // 设置查询条件
        CompassQueryBuilder queryBuilder = session.queryBuilder();
        CompassQueryBuilder.CompassBooleanQueryBuilder booleanQueryBuilder = queryBuilder.bool();

        booleanQueryBuilder.addMust(queryBuilder.queryString(q).useOrDefaultOperator().toQuery());

		// 设置过滤条件
        if (StringUtils.isNotBlank(categoryId)){
            CompassQuery categoryIdTerm = queryBuilder.term("category.ids", categoryId);
            booleanQueryBuilder.addMust(categoryIdTerm);
		}

        CompassSearchHelper searchHelper = new CompassSearchHelper(compass, page.getPageSize());
        CompassSearchResults results = searchHelper.search(new CompassSearchCommand(booleanQueryBuilder.toQuery(), page.getPageNo()));
        for (int i = 0; i < results.getHits().length; i++) {
            Article hit = (Article) results.getHits()[i].getData();
            list.add(dao.get(hit.getId()));
        }

        page.setList(list);
        page.setCount(results.getTotalHits());

        session.close();

        return page;
	}

    public void destroy(){
        compassTemplate.getCompass().close();
    }
}
